﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using WcfService1.DTO;

namespace MSIACCSL2.Views.SalesInvoice
{
    public partial class FrmAdd : Page
    {
        public SalesInvoiceViewModel VM
        {
            get
            {
               return this.DataContext as SalesInvoiceViewModel;
            }
        }

        public SalesInvoiceWrapViewModel WrapViewModel
        {
            get { return (this.Resources["POW"] as SalesInvoiceWrapViewModel); }
        }

        public FrmAdd()
        {
            InitializeComponent();
         
            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as SalesInvoiceWrapViewModel).SalesInvoiceViewModel =
                                        (SalesInvoiceViewModel)this.DataContext;
                               };

            this.Loaded += (o, e) =>
            {
                txtCustomer.Focus();
            };
           
            this.txtCustomer.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    //VM.GetCustomer(txtCustomer.Text);
                    CustomerSearch customerSearch = new CustomerSearch();
                    customerSearch.Closed += new EventHandler(customerSearch_Closed);
                    customerSearch.Show();
                    customerSearch.txtCustomerName.Text = txtCustomer.Text;
                }
            };

            this.KeyDown += (o, e) =>
            {

                if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.SaveSalesInvoice(VM.SalesInvoice);
                else if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.Cancel(null);
            };
        }

        void customerSearch_Closed(object sender, EventArgs e)
        {
            VM.SalesInvoice.Customer = (sender as CustomerSearch).Customer;
            txtCustomer.Focus();
        }

        private void textBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                WrapViewModel.GetInventory(((TextBox)sender).DataContext);
        }

        private void cboCostCenter_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cboCostCenter.SelectedItem != null && SalesInvoiceViewModel.mode != Mode.Edit)
                VM.GetLastNoSalesInvoice(cboCostCenter.SelectedValue);
            if (cboCostCenter.SelectedValue != null)
                VM.GetDeliveryOrderList(cboCostCenter.SelectedValue);
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }

        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void btnCloseStatus_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            VM.CloseStatus();
        }

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            //SalesInvoiceDetailDTO siDetail = (sender as TextBox).DataContext as SalesInvoiceDetailDTO;
            //if (siDetail != null)
            //    if (siDetail.Inventory.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", siDetail.Inventory.QtyUnit);
            //    }
            //    else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", siDetail.Inventory.QtyUnit);
            //    }
        }

        private void TextBox_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
          
        }

        private void DatePicker_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cboCostCenter.SelectedValue != null)
            {
                //VM.DeliveryOrders = new ObservableCollection<DeliveryOrderDTO>(VM.DeliveryOrders.Where(d => d.DODate <= VM.SalesInvoice.InvoiceDate));
                //if (VM.SalesInvoice.DeliveryOrder != null)
                //{
                //    if (! VM.DeliveryOrders.Any(x=>x.DONo == VM.SalesInvoice.DeliveryOrder.DONo))
                //        VM.DeliveryOrders.Add(VM.SalesInvoice.DeliveryOrder);
                //    VM.SalesInvoice.RaisePropertyChanged("DeliveryOrder");
                //}

                VM.GetDeliveryOrderList(VM.SalesInvoice.CostCenter);
            }
        }
     

    }
}

